﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.IO;

namespace ADA.Business
{
    public class BOLCommon
    {
        public static string getPostStatus(short status)
        {
            string postStatus = "";
            switch (status)
            {
                case 1:
                    postStatus = "processing";
                    break;
                case 2:
                    postStatus = "success";
                    break;
                case 3:
                    postStatus = "expired";
                    break;
                default:
                    break;
            }
            return postStatus;
        }

        public static bool SendMailMulti(
            string server,
            string userEmail,
            string userPass,
            int port,
            string displayName,
            string fromMail,
            string toMail,
            string ccMail,
            string bccMail,
            string subject,
            string content,
            bool useSSL,
            bool isHtmlContent)
        {
            try
            {
                var from = new MailAddress(fromMail, displayName);
                var listTo = toMail.Split(',');
                var message = new MailMessage();
                message.From = from;
                for (int i = 0; i < listTo.Length; i++)
                {
                    message.To.Add(listTo[i]);
                }
                message.Subject = subject;
                message.Body = content;
                message.BodyEncoding = Encoding.UTF8;
                message.Priority = MailPriority.High;
                message.IsBodyHtml = isHtmlContent;
                SmtpClient client = new SmtpClient(server);
                client.Credentials = new NetworkCredential(userEmail, userPass);
                client.EnableSsl = useSSL;
                client.Port = port;
                client.Send(message);
                return true;
            }
            catch (Exception ex)
            {
                using (System.IO.StreamWriter file = new System.IO.StreamWriter("~/SendMailErr.txt", true))
                {
                    var str =
                        server + Environment.NewLine +
                        userEmail + Environment.NewLine +
                        userPass + Environment.NewLine +
                        port + Environment.NewLine +
                        displayName + Environment.NewLine +
                        fromMail + Environment.NewLine +
                        toMail + Environment.NewLine +
                        ccMail + Environment.NewLine +
                        bccMail + Environment.NewLine +
                        subject + Environment.NewLine +
                        content;
                    file.WriteLine(Environment.NewLine + ex.Message + Environment.NewLine + ex.Source + Environment.NewLine + ex.StackTrace + Environment.NewLine + str);
                }

                return false;
            }
        }

        public static bool checkFileImage(string fileName)
        {

            string ext = Path.GetExtension(fileName);

            switch (ext.ToLower())
            {

                case ".gif":

                    return true;

                case ".png":

                    return true;

                case ".jpg":

                    return true;

                case ".jpeg":

                    return true;

                default:

                    return false;
            }
        }
    }
}
